package com.situ.mall.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.aspectj.weaver.NewConstructorTypeMunger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mysql.jdbc.log.Log;
import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Order;
import com.situ.mall.entity.OrderItem;
import com.situ.mall.entity.Result;
import com.situ.mall.entity.User;
import com.situ.mall.mapper.CartMapper;
import com.situ.mall.mapper.OrderItemMapper;
import com.situ.mall.mapper.OrderMapper;
import com.situ.mall.service.IOrderService;
import com.situ.mall.util.DateUtil;

@Service
public class OrderServiceImpl implements IOrderService{
	@Autowired
	private CartMapper cartMapper;
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;

	@Override
	public boolean insert(Order order) {
		// 现在这个order里面有userId、shippingId、paymentType
		// orderNo:使用当前的日期和时间 20200402093523
		order.setOrderNo(DateUtil.getCurrentTime());
		// TODO 不应该写10魔法数字，后期使用枚举
		// 订单状态:0-已取消-10-未付款，20-已付款，40-已发货，50-交易成功，60-交易关闭
		order.setStatus(10);
		// 现在只差payment: payment decimal(20,2)实际付款金额,单位是元,保留两位小数
		// 这个订单的实际付款金额需要遍历所有的订单项order_item
		
		double payment = 0.0;
		// 当前用户购物车里面勾选了checked=1要结算的购物车列表
		List<Cart> cartList = cartMapper.selectByUserIdAndChecked(order.getUserId());
		for (Cart cart : cartList) {
			OrderItem orderItem = new OrderItem();
			orderItem.setUserId(order.getUserId());
			orderItem.setOrderNo(order.getOrderNo());
			orderItem.setProductId(cart.getProductId());
			orderItem.setProductName(cart.getProduct().getName());// 快照
			orderItem.setProductImage(cart.getProduct().getMainImage());// 快照
			orderItem.setCurrentUnitPrice(cart.getProduct().getPrice());// 快照
			orderItem.setQuantity(cart.getQuantity());
			// total_price decimal(20,2)商品总价,单位是元,保留两位小数
			// 这个字段冗余，是多余的，完全可以通过单价*数量，但是如果经常用到这个字段
			// 每次去计算就非常麻烦，所以可以适当加冗余字段，方便操作
			// The operator * is undefined for the argument type(s) BigDecimal, Integer
			double totalPrice = cart.getProduct().getPrice().doubleValue() * cart.getQuantity();
			orderItem.setTotalPrice(new BigDecimal(totalPrice));
			
			payment += totalPrice;
			cartMapper.deleteByProductId(cart.getProductId());
			System.out.println("从购物车删除productID = " + cart.getProductId() + "成功");
			orderItemMapper.insert(orderItem);
		}
		
		order.setPayment(new BigDecimal(payment));
		int count = orderMapper.insert(order);
		
		return count == 1 ? true : false;
	}

	@Override
	public List<Order> selectByUserId(Integer userId) {
		//根据用户ID查找订单信息
		List<Order> list = orderMapper.selectByUserId(userId);
		return list;
	}

	@Override
	public List<OrderItem> selectOrderItemByOrderId(long orderNo) {
		// TODO Auto-generated method stub
		//根据订单Id查找内选项
		List<OrderItem> list = orderItemMapper.selectOrderItemByOrderId(orderNo);
		return list;
	}

	//后台查找
	@Override
	public Result selectByPage(Integer page, Integer limit) {
		System.out.println("OrderServiceImpl.selectByPage()");
		// 查询一种有多少条数据
		int count = orderMapper.getTotalCount();
		System.out.println("count = " +count);
		// 当前页数据
		int offset = (page -1 ) * limit;
		List<Order> list = orderMapper.selectByPage(offset, limit);
		for (Order order : list) {
			System.out.println(order);
		}
		return Result.success("", list, count);
	}

	@Override
	public boolean deleteById(Integer id,Long orderNo) {
		int count = orderMapper.deleteByPrimaryKey(id);
		if (count == 1) {//删除成功了，删除Item里的数据
			int count1 = orderItemMapper.deleteByOrderNo(orderNo);
		}
		return count == 1 ? true : false;
	}

	@Override
	public Order selectByOrderNo(Long orderNo) {
		Order order = orderMapper.selectByOrderNo(orderNo);
		return order;
	}

	@Override
	public boolean deleteAll(Integer[] ids, Long[] orderNos) {
		int countId = 0;
		for (Integer id : ids) {
			// 每删除一个影响一行c返回就是1
			int c = orderMapper.deleteByPrimaryKey(id);
			countId += c;
		}
		for (Long orderNo : orderNos) {
			// 每删除一个影响一行c返回就是1
			int c = orderItemMapper.deleteByOrderNo(orderNo);
		}
		System.out.println("countId = " + countId);
		System.out.println("ids.length = " + ids.length);
		//int count = productMapper.deleteAll(ids);
		return countId == ids.length ? true : false;
	}


}